package com.dao.Impl;

import com.dao.RechargeruleDao;
import com.entity.PageBean;
import com.entity.Rechargerule;
import com.util.JDBC;
import com.util.TransBean;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RechargeruleDaoImpl implements RechargeruleDao {
    @Override
    public int insert(Rechargerule rechargerule) {

        return JDBC.sqlUpdate("insert into rechargerule value(null,?,?,?,?,?,?)",
                rechargerule.getName(), rechargerule.getCoefficient(), rechargerule.getCreatedTime(), rechargerule.getEndTime(), rechargerule.getStatus(), rechargerule.getStartMoney());
    }

    @Override
    public int modify(Rechargerule rechargerule) {
        String  sql = "update rechargerule set name=?,coefficient=?,createdTime=?,endTime=?,status=?,startMoney=? where id=? ";
        return JDBC.sqlUpdate("sql", rechargerule.getName(), rechargerule.getCoefficient(), rechargerule.getCreatedTime(), rechargerule.getEndTime(), rechargerule.getStatus(), rechargerule.getStartMoney(), rechargerule.getId());
    }

    @Override
    public int delete(int id) {

                JDBC.sqlUpdate("delete from rechargerule where id = ?",id);
                return -1;
    }

    @Override
    public List<Rechargerule> findAll() {
        List<Map> list1= JDBC.selectMap("select id,name,coefficient,createdTime,endTime,status,startMoney from rechargerule");
        List<Rechargerule> list=TransBean.populate(Rechargerule.class,list1);
        return list;
    }

    //分页查询 我不会，唉，抄的
    @Override
    public void findByPage(PageBean<Rechargerule> pageBean) {
        //sql语句， 注意空格 ps：1=1 是为了存放条件，因为不能直接单写一个where
        String sql="select count(*) from rechargerule where 1=1 %s ";
        //将前台传来的数据妮成一个数组
        String[] factor = pageBean.getFactor();
        String whereStr = "";
        if (factor != null){
            for (String o : factor) {
                whereStr += " and " + o;
            }
        }
        //将 前台的条件加上本来的sql语句组成一个新的sql语句
        sql = String.format(sql,whereStr);
        List<List> lists =JDBC.selectArray(sql);
        Integer count = null;
        if (lists != null){
            count = ((Long) lists.get(0).get(0)).intValue();
        }
        sql = "select id,name,coefficient,createdTime,endTime,status,startMoney from rechargerule where 1=1 %s limit ?,?";
        sql = String.format(sql, whereStr);
        List<Map> mapList = JDBC.selectMap(sql, (pageBean.getPageNo() - 1) * pageBean.getPageSize(), pageBean.getPageSize());
        pageBean.setData(TransBean.populate(Rechargerule.class,mapList));
        pageBean.setRowCount(count);
    }

    @Override
    public Rechargerule findById(int id) {
        List<List> lists = JDBC.selectArray("select id,name,coefficient,createdTime,endTime,status,startMoney from rechargerule where id =?", id);
        if (lists.size()>0){
            return new Rechargerule(
                    (Integer) lists.get(0).get(0),
                    (String) lists.get(0).get(1),
                    (Double) lists.get(0).get(2),
                    (Date) (lists.get(0).get(3)),
                    (Date) (lists.get(0).get(4)),
                    (Integer) lists.get(0).get(5),
                    (Integer) lists.get(0).get(6)
            );
        }
        return null;
    }

    @Override
    public List<Rechargerule> findname(String name) {
        List<List> lists = JDBC.selectArray("select name,coefficient,createdTime,endTime,status,startMoney from rechargerule where name like '%" + name + "%' ");
        List<Rechargerule> rechargerules = new ArrayList<>();
        for (List o : lists) {
            rechargerules.add(new Rechargerule(
                    (Integer) o.get(0),
                    (String) o.get(1),
                    (Double) o.get(2),
                    (Date) o.get(3),
                    (Date) o.get(4),
                    (Integer) o.get(5),
                    (Integer) o.get(6)
            ));
        }
        return rechargerules;
    }


    /**
     * 分页查询
     * @param page
     */
    @Override
    public void findAllRule(PageBean page) {
        String sql="select count(*) from rechargerule where 1=1 ";
        String condstr="";
        if (page.getFactor()!=null){
            for (String s : page.getFactor()) {
                condstr+=" and "+s;
            }
        }

        page.setStartIndex();
        List<List> lists=JDBC.selectArray((sql+(condstr==null?"":condstr)));
        page.setRowCount(((Long)lists.get(0).get(0)).intValue());
        page.getTotalPage();
        sql=String.format("select id,name,coefficient,createdTime,endTime,status,startMoney from rechargerule where 1=1 %s limit ?,?",(condstr==null?"":condstr));
        List<Map> listList=JDBC.selectMap(sql,(page.getPageNo() - 1) * page.getPageSize(), page.getPageSize());
        if (listList.size()>0){
            page.setData(listList);
        }
    }

    @Override
    public void insertRule(Rechargerule rrule) {
        JDBC.sqlUpdate("insert into rechargerule(id,name,coefficient,createdTime,endTime,status,startMoney) values(null,?,?,?,?,?,?)",
                rrule.getName(),
                rrule.getCoefficient(),
                rrule.getCreatedTime(),
                rrule.getEndTime(),
                rrule.getStatus(),
                rrule.getStartMoney()
        );
    }

    @Override
    public void updateRule(Rechargerule rrule) {
        JDBC.sqlUpdate("update rechargerule set name=?,coefficient=?,createdTime=?,endTime=?,status=?,startMoney=? where id=?",
                rrule.getName(),
                rrule.getCoefficient(),
                rrule.getCreatedTime(),
                rrule.getEndTime(),
                rrule.getStatus(),
                rrule.getStartMoney(),
                rrule.getId()
                );
    }
}
